package per.chenyl.framework.persistence.wrapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AbstractWrapper<T> implements Wrapper<T> {
    protected T entity;
    protected StringBuilder condition = new StringBuilder();
    /**
     * 条件对应的值
     */
    protected List<Object> values = new ArrayList<>(16);

    public AbstractWrapper() {
    }

    public AbstractWrapper(T entity) {
        this.entity = entity;
    }



    {
        condition.append(" where 1=1 ");
    }

    @Override
    public Wrapper<T> eq(String column, Object value) {
        condition.append("and ").append(column).append("=").append("? ");
        values.add(value);
        return this;
    }

    @Override
    public Wrapper<T> ne(String column, Object value) {
        condition.append("and ").append(column).append("<>").append("? ");
        values.add(value);
        return this;
    }

    @Override
    public Wrapper<T> lt(String column, Object value) {
        condition.append("and ").append(column).append("<").append("? ");
        values.add(value);
        return this;
    }

    @Override
    public Wrapper<T> gt(String column, Object value) {
        condition.append("and ").append(column).append(">").append("? ");
        values.add(value);
        return this;
    }

    @Override
    public Wrapper<T> ge(String column, Object value) {
        condition.append("and ").append(column).append(">=").append("? ");
        values.add(value);
        return this;
    }

    @Override
    public Wrapper<T> le(String column, Object value) {
        condition.append("and ").append(column).append("<=").append("? ");
        values.add(value);
        return this;
    }


    @Override
    public Wrapper<T> like(String column, Object value, Like like) {
        condition.append("and ").append(column).append(" like ");
        switch (like) {
            case BEFORE:
                condition.append("'%?' ");
                break;
            case AFTER:
                condition.append("'?%' ");
                break;
            case ALL:
                condition.append("'%?%' ");
                break;
            default:
                break;
        }
        values.add(value);
        return this;
    }

    @Override
    public Wrapper<T> orderBy(String column, boolean desc) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Wrapper<T> batchOrderBy(HashMap<String, Boolean> columns) {
        throw new UnsupportedOperationException();
    }

    public enum Like {
        BEFORE,
        AFTER,
        ALL;
    }

    @Override
    public void setEntity(T entity) {
        this.entity = entity;
    }


    @Override
    public T getEntity() {
        return this.entity;
    }


    @Override
    public String getCondition() {
        return condition.toString();
    }

    @Override
    public Wrapper<T> select(String colums) {
        throw new UnsupportedOperationException();
    }
    @Override
    public Wrapper<T> limit(int coumt) {
        throw new UnsupportedOperationException();
    }


    @Override
    public Object[] getValues() {
        return values.toArray();
    }

    @Override
    public boolean isUseSelect() {
        throw new UnsupportedOperationException();
    }

    @Override
    public String getBatchSql() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Object[] getBatchDeleteValues() {
        throw new UnsupportedOperationException();
    }
}
